package com.zghhost.codedemo.other;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.*;

/**
 * @author guohua.zhang@zhaopin.com.cn
 * @Date 2019/6/27 10:09
 */
public class CountJobOnceNum {
    private static LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(1024);
    private static ExecutorService pool = new ThreadPoolExecutor(100, 100,
            0L, TimeUnit.MILLISECONDS,
            queue, Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args)throws Exception{
        File file = new File("/Users/wow/Downloads/job0703/job.csv");
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

        StringBuffer sb = new StringBuffer();
        int i=1;

        String line = null;
        while((line = br.readLine()) != null){
            while(queue.size() == 1024){
            }
            try{
                URL u = new URL("http://localhost:8081/jobrelease/SendPulsar");
                HttpURLConnection conn = (HttpURLConnection) u.openConnection();
                conn.setRequestMethod("POST");
                conn.setDoOutput(true);
                conn.getOutputStream().write(("jobNums="+line+"&OperateType=NEW").getBytes());
                System.out.println(conn.getResponseCode());
                conn.disconnect();
            }catch (Exception e){
                e.printStackTrace();
                System.out.println(500);
            }
        }

        pool.shutdown();
        while (true){
            Thread.sleep(500);
            if(pool.isTerminated()){
                System.exit(-1);
            }
        }
    }
}
